package cn.xyf.eduservice.service.impl;

import cn.xyf.eduservice.domain.EduCourse;
import cn.xyf.eduservice.domain.EduCourseDescription;
import cn.xyf.eduservice.domain.vo.CourseInfoVo;
import cn.xyf.eduservice.mapper.EduCourseMapper;
import cn.xyf.eduservice.service.EduCourseDescriptionService;
import cn.xyf.eduservice.service.EduCourseService;
import cn.xyf.servicebase.handler.MyException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程 服务实现类
 *
 * @author xuyinfeng
 * @since 2021-04-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse>
    implements EduCourseService {
  @Autowired private EduCourseDescriptionService eduCourseDescriptionService;

  @Override
  public void saveCourseInfo(CourseInfoVo courseInfoVo) {
    // 1.向课程表中添加基本信息
    EduCourse eduCourse = new EduCourse();
    BeanUtils.copyProperties(courseInfoVo, eduCourse);
    int insert = baseMapper.insert(eduCourse);
    if (0 == insert) throw new MyException(201, "添加课程信息失败");
    String cid = eduCourse.getId();

    // 2.向课程简介表中添加课程简介
    EduCourseDescription eduCourseDescription = new EduCourseDescription();
    eduCourseDescription.setDescription(courseInfoVo.getDescription());
    eduCourseDescription.setId(cid);
    eduCourseDescriptionService.save(eduCourseDescription);
  }
}
